<?php
class ListingTypesController extends AppController
{
	var $name = 'ListingTypes';
	var $helpers = array('Form','Html','Cms.Index');
	
	function beforeFilter()
	{		
		parent::beforeFilter();
	}
	
	function admin_add()
	{
		$this->pageTitle = __('Add');
		$this->__fetchListingTypeOptions();
		if(!empty($this->data))
		{
			if($this->save($this->data))
			{
				$this->successMsg(__('Listing type has been successfully added'));
				$this->redirect(array('controller'=>'listing_types','action'=>'admin_index'));
			}else
			{
				$this->errorMsg(__('Listing type could not be added!'));
			}
		}else
		{
			$this->ListingType->RateableField->setFieldProperties('is_required',array('selected'=>1));
			//prd($this->ListingType->RateableField->fieldProperties);
			
		}		
	}
	
	function __fetchListingTypeOptions()
	{
		$fieldProperties = $this->ListingType->fieldProperties;
		$optionList = $this->ListingType->Fieldset->getOptionList();
		$fieldsetOptionList['options'] = $optionList;
		$this->ListingType->setFieldProperties('Fieldset',$fieldsetOptionList);
				
	}
	
	function admin_edit($id = null)
	{
		$this->pageTitle = __('Edit');
		$this->__fetchListingTypeOptions();
		$listingTypeData = $this->ListingType->findById($id);
		$this->ListingType->id = $listingTypeData['ListingType']['id'];
		if(empty($listingTypeData))
		{
			$this->errorMsg(__('Invalid URL!'));
			$this->redirect(array('controller'=>'listing_types','action'=>'admin_index'));
		}
		if(!empty($this->data))
		{					
			if($this->save($this->data))
			{
				$this->successMsg(__('Listing type has been successfully updated'));
				$this->redirect(array('action'=>'view',$id));
			}else
			{
				$this->errorMsg(__('Listing type could not be updated.!'));
			}
		}
		if(empty($this->data))
		{
			$this->data = $listingTypeData;
		}
		
		$fieldsets = $this->ListingType->Fieldset->find('list');
		
		$this->set('action_type','edit');
		$this->set('fieldsets',$fieldsets);
		$this->render('admin_add');

	}
	
	protected function save($data = null)
	{			
		if(!is_numeric($this->ListingType->id))
		{
			$this->ListingType->create();
			return $this->ListingType->saveAll($data, array('validate'=>'first'));
		}
		else
		{
			$data['ListingType']['id'] = $this->ListingType->id;
			return $this->ListingType->saveAll($data, array('validate'=>'first'));
		}
	}
	
	
	function admin_index()
	{
		$this->pageTitle = __('ListingTypes');
		$this->paginate['limit'] = 10; 
		$listingTypes = $this->paginate();
		$this->set(compact('listingTypes'));
	}
	
	function admin_view($id = null)
	{
		$listingType = $this->ListingType->findById($id);
		if(empty($listingType))
		{
			$this->errorMsg(__('Invalid URL!'));
			$this->redirect(array('controller'=>'listing_types','action'=>'admin_index'));
		}
		$this->pageTitle = sprintf(__('%s listing type'),$listingType['ListingType']['name']);
		
		$this->set(compact('listingType'));
		
	}	
	
	function admin_delete($id = null)
	{
		$listingType = $this->ListingType->findById($id);
		if(empty($listingType))
		{
			$this->errorMsg(__('Invalid URL!'));
			$this->redirect(array('controller'=>'listing_types','action'=>'admin_index'));
		}
		
		if( $this->ListingType->del($listingType['ListingType']['id']))
		{
			$this->successMsg(__('Listing type has been successfully deleted.'));
		}else
		{
			$this->errorMsg(__('Listing type could not be deleted.'));
		}
		$this->redirect(array('controller'=>'listing_types','action'=>'admin_index'));
	}

	function admin_updateFieldOrder($listingTypeId)
	{

		$ordersString = $_POST['orders'];
		$orders = explode('|',$ordersString);
		$i = 1;
		foreach($orders as $fieldsetId)
		{
			$listingTypeFieldsetId = $this->ListingType->ListingTypeFieldset->field('id',array("listing_type_id"=>$listingTypeId,'fieldset_id'=>$fieldsetId));
			if($listingTypeFieldsetId)
			{
				$this->ListingType->ListingTypeFieldset->id = $listingTypeFieldsetId;
				$this->ListingType->ListingTypeFieldset->saveField('field_order',$i);
				$i++;
			}
		}
		$this->ListingType->refreshCustomFieldsCache($listingTypeId);
		$this->_stop();
	}
	
	function admin_setOrders($id)
	{
		$listingType = $this->ListingType->findById($id);
		if(empty($listingType))
		{
			$this->errorMsg(__('Invalid URL!'));
			$this->redirect(array('controller'=>'listing_types','action'=>'admin_index'));
		}
		$this->pageTitle = sprintf(__('Set field group order of %s listing type.'),$listingType['ListingType']['name']);
		$this->set('listingType',$listingType);
	}

}
?>